﻿using System;

namespace TemperatureWP.Utilities
{
    // Source: http://www.geekchamp.com/articles/wp7-loopingselector-in-depth--part1-visual-structure-and-api
    public class IntLoopingDataSource : LoopingDataSourceBase
    {
        private int _minValue;
        private int _maxValue;
        private int _increment;

        public IntLoopingDataSource()
        {
            MaxValue = 10;
            MinValue = 0;
            Increment = 1;
            SelectedItem = 0;
        }

        public int MinValue
        {
            get
            {
                return _minValue;
            }
            set
            {
                if (value >= MaxValue)
                {
                    throw new ArgumentOutOfRangeException("MinValue", "MinValue cannot be equal or greater than MaxValue");
                }
                _minValue = value;
            }
        }

        public int MaxValue
        {
            get
            {
                return _maxValue;
            }
            set
            {
                if (value <= MinValue)
                {
                    throw new ArgumentOutOfRangeException("MaxValue", "MaxValue cannot be equal or lower than MinValue");
                }
                _maxValue = value;
            }
        }

        public int Increment
        {
            get
            {
                return _increment;
            }
            set
            {
                if (value < 1)
                {
                    throw new ArgumentOutOfRangeException("Increment", "Increment cannot be less than or equal to zero");
                }
                _increment = value;
            }
        }

        public override object GetNext(object relativeTo)
        {
            var nextValue = Convert.ToInt32(relativeTo) + Increment;
            if (nextValue > MaxValue)
            {
                nextValue = MinValue;
            }
            return nextValue;
        }

        public override object GetPrevious(object relativeTo)
        {
            var prevValue = Convert.ToInt32(relativeTo) - Increment;
            if (prevValue < MinValue)
            {
                prevValue = MaxValue;
            }
            return prevValue;
        }
    }
}
